from flask import Flask,request,jsonify,Blueprint
import json
from db import  db1

bp_work = Blueprint('work',__name__)


@bp_work.route('/works/',methods=['POST','GET'])
def work():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form()

        try:
            sql = "insert into `work` (`name`) value ('%s') " % data['name']
            db1.update(sql)
            return jsonify({"code": 200, 'mse': '添加成功'})
        except:
            return jsonify({"code": 10010, 'mse': '添加失败'})
    else:
        sql = "select * from `work`"
        res = db1.find_all(sql)
        return jsonify({"code":200,'list':res})

@bp_work.route('/workss/',methods=['POST'])
def workss():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    print(data)
    workid = int(data['id'])
    
    try:
        sql = "update `work` set `parameter`='%s' where id=%d " %(data['parameter'],workid)
        db1.update(sql)
        return jsonify({"code": 200, 'mse': '添加成功'})
    
    except :
    
        return jsonify({"code":10011,'mse':'添加失败'})

@bp_work.route('/getparameter/')
def getparameter():
    id = int(request.args.get('id'))
    sql = "select * from `work` where id=%d" %(id)
    res = db1.find(sql)
    return jsonify({"code":200,'list':res})


